//
// Created by Administrator on 2021/8/10.
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

//Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    bool isValidBST(TreeNode *root) {
        return helper(root,LONG_LONG_MIN,LONG_LONG_MAX);
    }
    bool helper(TreeNode *node,long long  lower,long long upper){
        if (node == nullptr)
            return true;
        if (node->val <= lower or node->val >= upper )
            return false;
        // 缩减距离
        return helper(node->left,lower,node->val) and helper(node->right,node->val,upper);
    }
    // 也可以中序遍历 确定是不是升序序列
};

int main() {
    auto n1 = TreeNode(2), n2 = TreeNode(1), n3 = TreeNode(3);
    n1.left = &n2;
    n1.right = &n3;
    Solution solution;
    cout << solution.isValidBST(&n1) << endl;
    return 0;
}
